package com.shycloud.mido.member.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.member.api.entity.BasicGoodsInfo;
import com.shycloud.mido.member.api.entity.BasicProductInfo;
import com.shycloud.mido.member.mapper.BasicGoodsInfoMapper;
import com.shycloud.mido.member.mapper.BasicProductInfoMapper;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 后台管理获取商品信息表
 *
 * @author Every
 */
@RestController
@AllArgsConstructor
@RequestMapping("/goods")
public class GoodsController {

	/** 商品表   业务 mapper */
	private final BasicGoodsInfoMapper goodsInfoMapper;
	/** 商品类型 业务 mapper */
	private final BasicProductInfoMapper productInfoMapper;

	@PostMapping("saveProduct")
	@ApiOperation(value = "新增商品类型")
	public R saveProduct(@RequestBody BasicProductInfo productInfo) {
		return new R(productInfoMapper.insert(productInfo) == 1);
	}

	@PostMapping("updProduct")
	@ApiOperation(value = "更新商品类型")
	public R updProduct(@RequestBody BasicProductInfo productInfo) {

		if (productInfo.getId() == null) {
			return new R(CommonConstants.FAIL, "id参数为空", false);
		}

		return new R(productInfoMapper.updateById(productInfo) == 1);
	}

	@GetMapping("getProductList")
	@ApiOperation(value = "获取商品类型")
	public R getProductList() {
		return new R(productInfoMapper.selectList(new QueryWrapper<>()));
	}

	@GetMapping("getGoodsList")
	@ApiOperation(value = "获取商品列表", notes = "分页")
	public R getGoodsList(Integer current, Integer size, Integer productId) {
		return new R(goodsInfoMapper.getGoodsList(new Page(current, size), productId));
	}

	@GetMapping("getGoodsListByType")
	@ApiOperation(value = "通过商品类型获取商品", notes = "分页")
	public R getGoodsListByType(Integer productId) {
		return new R(goodsInfoMapper.getGoodsList(productId));
	}

	@PostMapping("addGoods")
	@ApiOperation(value = "新增商品")
	public R addGoods(@RequestBody BasicGoodsInfo goodsInfo) {
		return new R(goodsInfoMapper.insert(goodsInfo));
	}

	@PostMapping("updGoods")
	@ApiOperation(value = "更新商品")
	public R updGoods(@RequestBody BasicGoodsInfo goodsInfo) {
		return new R(goodsInfoMapper.updateById(goodsInfo));
	}

	@DeleteMapping("delGoodsType/{id}")
	@ApiOperation(value = "删除商品类型")
	public R delGoodsType(@PathVariable("id") Integer id) {
		//查询此类型是否应用到商品当中
		Integer count = goodsInfoMapper.selectCount(new QueryWrapper<BasicGoodsInfo>().eq("product_id", id));
		if (count > 0) {
			return new R(CommonConstants.FAIL, "此商品类型下已有商品，无法删除", false);
		}
		return new R(productInfoMapper.deleteById(id));
	}

	@GetMapping("getGoodsByIdFeign")
	public BasicGoodsInfo getGoodsByIdFeign(Integer id) {
		return goodsInfoMapper.selectById(id);
	}

	@PostMapping("addGoodsInfoFeign")
	@ApiOperation(value = "新增商品")
	public BasicGoodsInfo addGoodsInfoFeign(@RequestBody BasicGoodsInfo goodsInfo) {
		goodsInfoMapper.insert(goodsInfo);
		return goodsInfo;
	}

}
